package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import butterknife.BindView;
import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.BucaoCategoryAdapter;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.BucaoCategoryBean;
import com.dlc.beijingrfid.net.protocol.StockProto;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 库存,布草分类，物品统计跳到的界面、分类的父类
 */
public class InventoryCategoryActivity extends BaseActivity {

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;
    private BucaoCategoryAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.leftExit(this);

        mAdapter = new BucaoCategoryAdapter();
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                int position) {

                BucaoCategoryBean.DataBean item = mAdapter.getItem(position);
                Intent intent = new Intent(InventoryCategoryActivity.this,InventoryNextActivity.class);
                intent.putExtra("id",String.valueOf(item.id));
                startActivity(intent);
                
            }
        });

        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);

        getCategory();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_inventory_category;
    }

    /**
     * 获取分类
     */
    private void getCategory() {

        StockProto.get()
            .categorys("0","")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<BucaoCategoryBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtil.show(getActivity(), e.getMessage());
                }

                @Override
                public void onNext(BucaoCategoryBean bucaoCategoryBean) {
                    mAdapter.setNewData(bucaoCategoryBean.data);
                }
            });
    }
}
